﻿using Bll;
using Help;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Myproject.BSM.Ashx.PcBase
{
    /// <summary>
    /// ContactAjax 的摘要说明
    /// </summary>
    public class ContactAjax : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (!string.IsNullOrEmpty(cookieHelp.AdminId))
            {
                if (new AdminBLL().ExistsByAdminName(cookieHelp.AdminName) && new AdminBLL().ExistsByAdminId(cookieHelp.AdminId))
                {

                    string dos = context.Request.Params["dos"];
                    string id;
                    #region swith do
                    switch (dos)
                    {
                        case "delete":
                            id = context.Request.QueryString["id"];
                            if (new ContactBLL().Delete(int.Parse(id)))
                            {
                                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "删除成功！", "pc_base_contactList", "", "", false, "", ""));
                            }
                            else
                            {
                                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "删除失败！", "pc_base_contactList", "", "", false, "", ""));
                            }
                            break;

                        case "rank":
                            id = context.Request.QueryString["id"];
                            string rank = context.Request.QueryString["rank"];
                            if (id != "0")
                            {
                                ContactModel model = new ContactBLL().GetModel(int.Parse(id));
                                model.Rank += int.Parse(rank);
                                if (new ContactBLL().Update(model))
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "修改排序成功！", "pc_base_contactList", "", "", false, "", ""));
                                }
                                else
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改排序失败！", "pc_base_contactList", "", "", false, "", ""));
                                }
                            }
                            else
                            {
                                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改排序失败！", "pc_base_contactList", "", "", false, "", ""));
                            }
                            break;

                        case "show":
                            id = context.Request.QueryString["id"];
                            string show = context.Request.QueryString["show"];
                            if (id != "0")
                            {
                                ContactModel model = new ContactBLL().GetModel(int.Parse(id));
                                model.Show = bool.Parse(show);
                                if (new ContactBLL().Update(model))
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "修改成功！", "pc_base_contactList", "", "", false, "", ""));
                                }
                                else
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改失败！", "pc_base_contactList", "", "", false, "", ""));
                                }
                            }
                            else
                            {
                                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改失败！", "pc_base_contactList", "", "", false, "", ""));
                            }
                            break;

                        case "edit":
                            id = context.Request.Params["pc_base_ContactEdit_ContactID"].Trim();
                            string name = context.Request.Params["pc_base_ContactEdit_name"].Trim();
                            string ename = context.Request.Params["pc_base_ContactEdit_ename"].Trim();
                            string post = context.Request.Params["pc_base_ContactEdit_post"].Trim();
                            string epost = context.Request.Params["pc_base_ContactEdit_epost"].Trim();
                            string phone = context.Request.Params["pc_base_ContactEdit_phone"].Trim();
                            string tel = context.Request.Params["pc_base_ContactEdit_tel"].Trim();
                            string email = context.Request.Params["pc_base_ContactEdit_email"].Trim();
                            string rank2 = context.Request.Params["pc_base_ContactEdit_rank"].Trim();
                            string show2 = context.Request.Params["pc_base_ContactEdit_show"].Trim();
                            if (id == "0")
                            {
                                ContactModel model = new ContactModel();
                                model.Name = name;
                                model.EName = ename;
                                model.Post = post;
                                model.EPost = epost;
                                model.MobilePhone = phone;
                                model.OfficePhone = tel;
                                model.Email = email;
                                model.Rank = int.Parse(rank2);
                                model.Show = bool.Parse(show2);

                                if (new ContactBLL().Add(model))
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "新增联系方式成功！", "pc_base_contactList", "", "", true, "", ""));
                                }
                                else
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "新增联系方式失败！", "pc_base_contactList", "", "", true, "", ""));
                                }
                            }
                            else
                            {
                                ContactModel model = new ContactBLL().GetModel(Int32.Parse(id));
                                model.Name = name;
                                model.EName = ename;
                                model.Post = post;
                                model.EPost = epost;
                                model.MobilePhone = phone;
                                model.OfficePhone = tel;
                                model.Email = email;
                                model.Rank = int.Parse(rank2);
                                model.Show = bool.Parse(show2);
                                if (new ContactBLL().Update(model))
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "修改联系方式成功！", "pc_base_contactList", "", "", true, "", ""));
                                }
                                else
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改联系方式失败！", "pc_base_contactList", "", "", true, "", ""));
                                }
                            }
                            break;
                    }
                    #endregion
                }
                else
                {
                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(301, "登陆超时！", "", "", "", false, "/bsm/logFrom.aspx", "登陆超时！"));
                }
            }
            else
            {
                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(301, "登陆超时！", "", "", "", false, "/bsm/logFrom.aspx", "登陆超时！"));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}